﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;

namespace YouHP.Pages.IndicatorHistory
{
    public class SurveyAdd : PatientBasePage
    {

        protected List<LabcheckInfo> IndicatorDataList;

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected new void HandleCommon()
        {
            this.AddLocation("指标记录", "/IndicatorHistory/Index.aspx");
            this.AddLocation("批量更新", "/IndicatorHistory/SurveyAdd.aspx");
            this.SetTitle("批量更新指标记录");

            //直接从病人数据中取出DiseaseId
            IndicatorDataList = LabcheckBLL.Instance.GetLabcheckList(PrData.DiseaseId);
            for (int i = 0; i < IndicatorDataList.Count; i++)
            {
                if (IndicatorDataList[i].IsQuantified > 0)
                {
                    //解析正常值字符串
                    string[] normalValues = IndicatorDataList[i].NormalValues.Split('@');
                    if (normalValues.Length >= 2)
                    {
                        IndicatorDataList[i].NormalMinValue = Convert.ToDouble(normalValues[0]);
                        IndicatorDataList[i].NormalMaxValue = Convert.ToDouble(normalValues[1]);
                    }

                    //解析上下限字符串
                    string[] limitValues = IndicatorDataList[i].LimitValues.Split('@');
                    if (limitValues.Length >= 2)
                    {
                        IndicatorDataList[i].MinValue = Convert.ToDouble(limitValues[0]);
                        IndicatorDataList[i].MaxValue = Convert.ToDouble(limitValues[1]);
                    }
                }
            }
        }

        protected new void HandleGet()
        {
           
        }

        protected new void HandlePost()
        {
            List<IndicatorHistoryInfo> lst = ValidateForm();
            if (lst == null) return;
            bool succ = true;

            PatientIndicatorBLL.Instance.AddIndicatorSurvey(lst);
            if (succ)
            {
                this.AddMsgLine("添加成功");
            }
            else
            {
                this.AddErrLine("添加失败");
            }

            this.SetUrl("/IndicatorHistory/Index.aspx");
            this.SetMetaRefresh();
        }

        protected List<IndicatorHistoryInfo> ValidateForm()
        {
            // 对时间进行判断
            string value = RequestTools.GetFormString("date");
            if (value == "")
            {
                this.AddErrLine("请填写日期！");
                return null;
            }

            DateTime date;
            bool succ = DateTime.TryParse(value, out date);
            if (succ == false)
            {
                this.AddErrLine("错误的日期：" + value);
                return null;
            }

            if (DateTime.Compare(date, DateTime.Today) > 0)
            {
                this.AddErrLine("记录日期 " + date.ToString("yyyy-MM-dd") + " 不能比当前日期 " + DateTime.Today.ToString("yyyy-MM-dd") + " 晚！");
                return null;
            }
            else if (DateTime.Compare(date, PrData.StartDate) < 0)
            {
                this.AddErrLine("记录日期 " + date.ToString("yyyy-MM-dd") + " 不能比第一次发病日期 " + PrData.StartDate.ToString("yyyy-MM-dd") + " 早！");
                return null;
            }

            List<IndicatorHistoryInfo> lst = new List<IndicatorHistoryInfo>();
            foreach (LabcheckInfo item in IndicatorDataList)
            {
                string strIdcValue = RequestTools.GetFormString("idcvalue" + item.Id);
                if(strIdcValue.Length > 0)
                {
                    IndicatorHistoryInfo info = new IndicatorHistoryInfo();
                    info.PRId = PrData.Id;
                    info.IndicatorId = item.Id;
                    info.DataValue = Utils.StrToDouble(strIdcValue, item.MinValue);
                    if (info.DataValue <= item.MinValue)
                    {
                        this.AddErrLine("请输入指标 （" + item.Name + "） 的有效数值！");
                        return null;
                    }

                    info.RecordDate = date;
                    info.UpdateDate = DateTime.Today;

                    lst.Add(info);
                }
            }

            if(lst.Count <= 0)
            {
                this.AddErrLine("请输入至少一个有效的指标值然后提交！");
                return null;
            }

            return lst;
        }
    }
}